package com.ck.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ck.entity.MRoom;
import com.ck.entity.MRoomUser;
import com.ck.entity.MUser;
import com.ck.mapper.MRoomMapper;
import com.ck.mapper.MRoomUserMapper;
import com.ck.service.MRoomService;
import com.ck.vo.MRoomVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 房间表(MRoom)表服务实现类
 *
 * @author ck
 * @since 2020-11-06 15:44:01
 */
@Service("mRoomService")
public class MRoomServiceImpl extends ServiceImpl<MRoomMapper, MRoom> implements MRoomService {

    @Resource
    private MRoomMapper mRoomMapper;
    @Resource
    private MRoomUserMapper mRoomUserMapper;

    @Transactional
    @Override
    public Boolean createRoom(MRoomVO mRoomVO) {
        int i=0;
        boolean flag = true;
        List<MUser> list = mRoomVO.getUserList();
        int len=list.size();
        if(list != null && len != 0){
            if(len == 1){
                //如果list.size() 为1的话, 说明只有1一个人, 即创建房间
                i = mRoomMapper.insert(mRoomVO);
            }
            if(i>0){
                insertRoomUser(mRoomVO.getId(),list);
            }else{
                flag = false;
            }
        }else{
            flag = false;
        }
        return flag;
    }

    /**
     * 插入房间用户
     * @param roomId
     * @param list
     */
    private void insertRoomUser(int roomId,List<MUser> list){
        MRoomUser mRoomUser;
        for(MUser mUser:list){
            mRoomUser = new MRoomUser();
            mRoomUser.setRoomId(roomId);
            mRoomUser.setAllCoin(new BigDecimal(0));
            mRoomUser.setUserId( mUser.getId());
            mRoomUserMapper.insert(mRoomUser);
        }
    }
}
